package com.tbit.uqbike.client.quartz;

import com.tbit.uqbike.client.constant.SystemData;
import com.tbit.uqbike.client.dao.core.MachineDao;
import com.tbit.uqbike.client.pojo.Machine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chen
 * @Description: 设备同步定时任务
 * @Date: 创建于 14:27 2021/5/25
 */
@Component
@Slf4j
public class MachineQuartz {
    @Autowired
    private MachineDao machineDao;

    @Scheduled(cron = "0 0/30 * * * ?")
    public void getMachines() {
        Map<String, Machine> machineMap = new HashMap<>();
        List<Machine> machines = machineDao.getAll();
        if (machines.size() > 0) {
            for (Machine machine : machines) {
                machineMap.put("machineId." + machine.getMachineId(), machine);
                machineMap.put("userCode." + machine.getUserCode(), machine);
                machineMap.put("machineNO." + machine.getMachineNO(), machine);
            }
        }
        log.info("同步设备信息[" + machines.size() + "]");
        SystemData.machineInfoMap = machineMap;
    }

}
